package de.hmm.hwip.connection.service;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import android.util.Log;
import de.hmm.hwip.connection.api.IConIn;
import de.hmm.hwip.connection.api.IConOut;
import de.hmm.hwip.connection.input.InConImpl;
import de.hmm.hwip.connection.output.OutConImpl;
/**
 * OSGi network service
 * Gets data via Interface
 * 
 * @author johannes mendel
 * @version 24.10.2012
 *
 */
public class Activator implements BundleActivator{

	/**
	 * Class identifier to debug
	 */
	private String TAG = "Con. Service Activator";	
	
	/**
	 * OSGi-Service Interface IConIn for incoming Connections
	 */
	private IConIn inService;
	
	/**
	 * OSGi-Service Interface IConIn for outgoing Connections
	 */
	private IConOut outService;
	
	/**
	 * start Osgi-Service
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		Log.i(TAG, "OSGi-Service - Network-Connection - started");
		inService = new InConImpl(context);
		outService = new OutConImpl();
		
        // register the incoming service
        Hashtable<String, Object> paramsIn = new Hashtable<String, Object>();
        paramsIn.put( org.osgi.framework.Constants.SERVICE_PID, IConIn.class.getName());
        paramsIn.put( org.osgi.framework.Constants.SERVICE_DESCRIPTION,"Provides incomming connection Service");
        context.registerService(IConIn.class.getName(), inService, paramsIn);
        
        // register the outgoing service
        Hashtable<String, Object> paramsOut = new Hashtable<String, Object>();
        paramsOut.put( org.osgi.framework.Constants.SERVICE_PID, IConOut.class.getName());
        paramsOut.put( org.osgi.framework.Constants.SERVICE_DESCRIPTION,"Provides outgoing connection Service");
        context.registerService(IConOut.class.getName(), outService, paramsOut);
    
        //new Thread to wait for servicetracker and not to stop the hole system
        ConfigurationThread confiThread = new ConfigurationThread(context);
        Thread ct = new Thread(confiThread);
        ct.start();
     
	}

	/**
	 * stop OSGi-Service
	 */
	@Override
	public void stop(BundleContext arg0) throws Exception {
		//RestServerImpl.stop();
		Log.i(TAG, "OSGi-Service - Network-Connection-Service - stopped");		
	}
	
}
